<?php

namespace app\mec\controller;
use think\Db;
use app\admin\model\UserModel;
use app\admin\model\UserType;
use think\Session;

class Device extends Base
{

    /**
     * [index 用户列表]
     * @return [type] [description]
     * @author
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            $map['a.shop_fid'] = session('mid');
            if(isset($key)&&$key!="")
            {
                $map['a.shop_name|a.shop_username|a.shop_device_id'] = ['like',"%" . $key . "%"];
            }
            if(isset($level)&&$level!="")
            {
                $map['a.shop_shi'] = $level;
            }
            if(isset($feilv) && $feilv!="")
            {
                if($feilv == 20){
                    $map['a.shop_feilv'] = ['>',20];
                }else{
                    $map['a.shop_feilv'] = ['<=',20];
                }
            }
            if(isset($jihuo) && $jihuo!="")
            {
                $map['a.shop_status'] = $jihuo;
            }

            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::table('up_shop_device')->alias('a')
                ->join('up_area aa','a.shop_shi=aa.district_id','LEFT')
                ->where($map)
                ->count();
            $lists = Db::table('up_shop_device')->alias('a')
                ->join('up_area aa','a.shop_shi=aa.district_id','LEFT')
                ->where($map)
                ->order('shop_id desc')
                ->page($Nowpage, $limits)
                ->select();//计算总页面
            $rename = Db::name('fenxiao')->where('fen_id',session('mid'))->value('fen_realname');
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['fenname'] = $rename;
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch("device/index");
    }


    /**
     * [userAdd 添加用户]
     * @return [type] [description]
     * @author
     */
    public function fenxiaoAdd()
    {
        if(request()->isPost()){
            $param = input('post.');
            $param['shop_feilv'] = (int)$param['shop_feilv'];
            if($param['shop_feilv'] < 0 || $param['shop_feilv'] > 60){
                return json(['code' => 100, 'msg' => '费率不能低于0且不能高于60']);
            }
            $have = Db::table('up_shop_device')
                ->where('shop_device_id',$param['shop_device_id'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '当前设备号已存在']);
            }
            if($param['shop_mchid']){
                $newnew = new \app\api\controller\Weixin();
                $back = $newnew->addFen($param['shop_mchid']);
                if($back['status'] != 200){
                    return json(['status'=>0,'msg'=>'请检查商户号正确性']);
                }
            }
            $param['shop_fid'] = session('mid');
            $param['shop_time'] = date('Y-m-d H:i:s');
            $res2 = Db::table('up_shop_device')->insert($param);
            if($res2){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败，请刷新重试']);
            }
        }
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch('device/useradd');
    }


    /**
     * [userEdit 编辑用户]
     * @return [type] [description]
     * @author
     */
    public function userEdit()
    {
        if(request()->isPost()){
            $param = input('post.');
//            $have = Db::table('up_shop_device')
//                ->where('shop_id',$param['shop_id'])
//                ->find();
//            if($have['shop_status'] == 2){
//                return json(['code' => 100, 'msg' => '当前设备号已被激活，不能更改']);
//            }
            $param['shop_feilv'] = (int)$param['shop_feilv'];
            if($param['shop_feilv'] < 0 || $param['shop_feilv'] > 60){
                return json(['code' => 100, 'msg' => '费率不能低于0且不能高于60']);
            }
            if($param['shop_mchid']){
                $newnew = new \app\api\controller\Weixin();
                $back = $newnew->addFen($param['shop_mchid']);
                if($back['status'] != 200){
                    return json(['status'=>0,'msg'=>$back['msg']]);
                }
            }

            $have = Db::table('up_shop_device')
                ->where('shop_id','<>',$param['shop_id'])
                ->where('shop_device_id',$param['shop_device_id'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '当前设备号已存在']);
            }
            $param['shop_time'] = date('Y-m-d H:i:s');
            $res2 = Db::table('up_shop_device')->where('shop_id',$param['shop_id'])->update($param);
            if($res2){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败，请刷新重试']);
            }
        }
        $id = input('param.id');
        $user = Db::table('up_shop_device')->where('shop_id',$id)->find();
        $this->assign('user',$user);
        $citys = Db::table('up_area')->where('level',2)->select();
        $this->assign('citys',$citys);
        return $this->fetch("device/useredit");
    }

}